package com.linkoog.devtools.http.request.http_send;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.JBPopupListener;
import com.intellij.openapi.ui.popup.LightweightWindowEvent;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.JBColor;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.scale.JBUIScale;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.StartupUiUtil;
import com.linkoog.devtools.http.common.HttpMethod;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;

public class HttpMethodLabel extends JBLabel {

    @Getter
    private HttpMethod httpMethod;
    private JBPopup myMethodPopup;



    public HttpMethodLabel() {
        this.setFont(StartupUiUtil.getLabelFont().deriveFont(1));
        this.setIcon(AllIcons.General.LinkDropTriangle);
        this.setIconTextGap(JBUIScale.scale(1));
        this.setHorizontalTextPosition(10);
        this.setOpaque(false);
        setPreferredSize(new Dimension(85, -1));
        setHttpMethod(HttpMethod.GET);


        this.setBorder(new CompoundBorder(JBUI.Borders.customLine(JBColor.LIGHT_GRAY, 0, 0, 0, 1), JBUI.Borders.empty(0, 4, 0, 10)));
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(final MouseEvent mouseEvent) {
                showMethodPopup();
            }
        });
    }

    public void setHttpMethod(@NotNull HttpMethod httpMethod){
        this.httpMethod = httpMethod;
        this.setText(httpMethod.getName());
        this.setForeground(Color.decode(httpMethod.getColor()));
    }


    private void showMethodPopup() {
        if (this.myMethodPopup != null) {
            final JBPopup myMethodPopup = this.myMethodPopup;
            myMethodPopup.cancel();
            this.myMethodPopup = null;
        }

        this.myMethodPopup = JBPopupFactory.getInstance()
                .createPopupChooserBuilder(Arrays.asList(HttpMethod.values()))
                .setMovable(false)
                .setRequestFocus(true)
                .setItemChosenCallback(this::setHttpMethod)
                .setRenderer(new MyListCellRenderer()).createPopup();

        this.myMethodPopup.addListener(new JBPopupListener() {
            public void onClosed(@NotNull LightweightWindowEvent lightweightWindowEvent) {
                HttpMethodLabel.this.myMethodPopup = null;
            }
        });
        this.myMethodPopup.showUnderneathOf(this);
    }



    private static class MyListCellRenderer extends ColoredListCellRenderer<HttpMethod> {

        @Override
        protected void customizeCellRenderer(@NotNull JList<? extends HttpMethod> list, HttpMethod value, int index, boolean selected, boolean hasFocus) {
            //setIcon(value.getIcon());
            append(value.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
        }


    }

}
